package com.sosee.cgfw.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.navi.model.NaviLatLng;
import com.amap.api.services.geocoder.GeocodeAddress;
import com.amap.api.services.geocoder.GeocodeQuery;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.sosee.cgfw.R;
import com.sosee.cgfw.activity.LoginActivity;
import com.sosee.cgfw.activity.ScheduleDetailActivity;
import com.sosee.cgfw.entity.JsonData;
import com.sosee.cgfw.gaodemap.NewTestActivity;
import com.sosee.cgfw.interfac.ScheduleInterface;
import com.sosee.cgfw.utils.Constans;
import com.sosee.cgfw.utils.LogUtils;
import com.sosee.cgfw.utils.Utils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2016/2/1.
 */
public class ScheduleAdapter extends BaseAdapter {
    private List<JsonData> list;//数据集合
    private ScheduleInterface anInterface;//自定义接口
    private HttpUtils httpUtils=new HttpUtils();
    private Context context;
    private SharedPreferences sp;//本地提醒 存储
    private List<String> temList;//设置本地提醒的 秀场 id

    public String getRemind() {
        return remind;
    }

    public void setRemind(String remind) {
        this.remind = remind;
    }

    private String remind;//收藏 提醒 等

    public int getFrom() {
        return from;
    }

    public void setFrom(int from) {
        this.from = from;
    }

    private int from;//标志 从哪个页面跳转

    public ScheduleInterface getAnInterface() {
        return anInterface;
    }

    public void setAnInterface(ScheduleInterface anInterface) {
        this.anInterface = anInterface;
    }

    public List<JsonData> getList() {
        return list;
    }

    public void setList(List<JsonData> list) {
        this.list = list;
    }

    private LayoutInflater inflater;
    public ScheduleAdapter(Context context){
        sp=context.getSharedPreferences(Constans.ALARM,Context.MODE_PRIVATE);
        String oid=sp.getString(Constans.ALARM, "");
        temList =Utils.stringToList(oid, ",");
        inflater=LayoutInflater.from(context);
        this.context=context;
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if(view==null){
            view=inflater.inflate(R.layout.item_schedule,null);
            holder=new ViewHolder();
            holder.tv_start_time= (TextView) view.findViewById(R.id.tv_start_time);
            holder.tv_address= (TextView) view.findViewById(R.id.tv_address);
            holder.tv_address_e= (TextView) view.findViewById(R.id.tv_address_e);
            holder.tv_company= (TextView) view.findViewById(R.id.tv_compan);
            holder.tv_company_e= (TextView) view.findViewById(R.id.tv_company_e);
            holder.tv_info= (TextView) view.findViewById(R.id.tv_info);
            holder.tv_info_e= (TextView) view.findViewById(R.id.tv_info_e);
            holder.ig_menu= (ImageView) view.findViewById(R.id.ig_menu);
            holder.rl_menu= (RelativeLayout) view.findViewById(R.id.rl_nemu);
            holder.ll_menu= (RelativeLayout) view.findViewById(R.id.ll_menu);
            holder.tv_shoucang= (TextView) view.findViewById(R.id.tv_shoucang);
            holder.tv_remind= (TextView) view.findViewById(R.id.tv_remind);
            view.setTag(holder);

        }else{
            holder= (ViewHolder) view.getTag();
        }
        final JsonData data=list.get(i);
        String time=data.getData().getEnter_time();

        holder.tv_start_time.setText(time.substring(0,time.length()-3));
        //是否已提醒
        if(temList.contains(data.getData().getID())){
            holder.tv_remind.setText("已提醒");
        }
        holder.tv_info.setText(data.getData().getTitle());
        holder.tv_info_e.setText(data.getData().getTitle_e());
        holder.tv_address.setText(data.getData().getAddressA());
        holder.tv_address_e.setText(data.getData().getAddressA_e());
        holder.tv_company.setText(data.getData().getUndertake());
        holder.tv_company_e.setText(data.getData().getUndertake_e());
        holder.rl_menu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                anInterface.onZhankaiClick(i,true);
            }
        });
        final String id=data.getData().getID();
        //设置item北京颜色
        if(i%2==0){
            view.setBackgroundResource(R.drawable.richengkuang1);
        }else{
            view.setBackgroundResource(R.drawable.richengkuang2);
        }
        //设置菜单选项是否展开
        if(Utils.list_menu.contains(i+"")){
//            holder.rl_menu.setVisibility(View.INVISIBLE);
//            holder.ll_menu.setVisibility(View.VISIBLE);
            holder.ll_menu.findViewById(R.id.rl_shouqi).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
//                    anInterface.onZhankaiClick(i,false);
                }
            });
        }else{
//            holder.rl_menu.setVisibility(View.VISIBLE);
//            holder.ll_menu.setVisibility(View.INVISIBLE);
        }
        //收藏按钮
        final ImageView ig_shoucang=
                (ImageView) holder.ll_menu.findViewById(R.id.ig_shoucang);

        //提醒收藏
        if(Utils.list_shoucang.contains(id)){
            ig_shoucang.setImageResource(R.drawable.yishoucang);
            holder.tv_shoucang.setText("已收藏");
        }else{
            ig_shoucang.setImageResource(R.drawable.shoucang);
            holder.tv_shoucang.setText("");
        }
        //收藏点击事件
        ig_shoucang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constans.userID!=null&&!Constans.userID.trim().equals("")){
                    httpUtils.send(HttpRequest.HttpMethod.GET, Constans.URL_SHOUCANG+Constans.userID + "&showID=" + id, new RequestCallBack<String>() {
                        @Override
                        public void onSuccess(ResponseInfo<String> responseInfo) {
                            String json = responseInfo.result;
                            String status = null;
                            try {
                                JSONObject jsonObject = new JSONObject(json);
                                status = jsonObject.getString("status");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            } finally {
                                if ("success".equals(status)) {
                                    if (Utils.list_shoucang.contains(id)) {
                                        Utils.list_shoucang.remove(id);
                                        LogUtils.log_E(Utils.list_shoucang.toString());
                                        ig_shoucang.setImageResource(R.drawable.shoucang);
                                        LogUtils.log_E(Utils.list_shoucang.toString());
                                        Utils.showToast(context.getString(R.string.shoucang_cancle_success));
                                    } else {
                                        Utils.showToast(context.getString(R.string.shoucang_success));
                                        Utils.list_shoucang.add(id);
                                        LogUtils.log_E(Utils.list_shoucang.toString());
                                        ig_shoucang.setImageResource(R.drawable.yishoucang);
                                    }

                                } else {
                                    LogUtils.log_E("收藏信息："+json);
                                    if (Utils.list_shoucang.contains(id)) {
                                        Utils.showToast(context.getString(R.string.shoucang_cancle_failed));
                                    } else {
                                        Utils.showToast(context.getString(R.string.shoucang_failed));
                                    }
                                }
                            }
                            anInterface.onZhankaiClick(1000,true);

                        }

                        @Override
                        public void onFailure(HttpException e, String s) {
                            Utils.showToast(context.getString(R.string.shoucang_failed));
                        }
                    });
                }else{
                    context.startActivity(new Intent(context, LoginActivity.class));
                    ((Activity)context).finish();
                }


            }
        });
        //提醒按钮
        ImageView ig_tixing= (ImageView) holder.ll_menu.findViewById(R.id.ig_tixing);
        ig_tixing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,ScheduleDetailActivity.class);
                int remin=1;
                intent.putExtra("remind",remin);
                intent.putExtra("from",from);
                JsonData data= (JsonData) list.get(i);
                intent.putExtra("detail",data);
                context.startActivity(intent);
                ((Activity)context).finish();
            }
        });
        //导航按钮
        ImageView ig_daohang= (ImageView) holder.ll_menu.findViewById(R.id.ig_daohang);
        ig_daohang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent=new Intent(context,ScheduleDetailActivity.class);
//                JsonData data= (JsonData) list.get(i);
//                intent.putExtra("from",from);
//                intent.putExtra("detail",data);
//                context.startActivity(intent);
//                ((Activity)context).finish();
                address_query=list.get(i).getData().getAddressB();



                //导航功能 待实现
                testSearch();
            }
        });

        //申请按钮
        ImageView ig_shenqing= (ImageView) holder.ll_menu.findViewById(R.id.ig_shenqing);
        ig_shenqing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(Constans.userID!=null&&Constans.userID.trim().equals("")){
                    Intent intent=new Intent(context,ScheduleDetailActivity.class);
                    JsonData data= (JsonData) list.get(i);
                    intent.putExtra("detail",data);
                    intent.putExtra("from",from);
                    context.startActivity(intent);
                }else{
                    context.startActivity(new Intent(context,LoginActivity.class));
                }

                ((Activity)context).finish();
            }
        });




        return view;
    }

    //地理反编码
    List<GeocodeAddress> glist = new ArrayList<GeocodeAddress>();

    public void testSearch() {

        GeocodeSearch mSearch = new GeocodeSearch(context);
        mSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
            @Override
            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {

            }

            @Override
            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
                glist = geocodeResult.getGeocodeAddressList();
                //未搜索到结果 处理
                if (glist != null) {
                } else {
                    Utils.showToast("网络未连接");
                    return;
                }
                NaviLatLng end = null;
                if (glist.get(0).getLatLonPoint() == null) {
                } else {
                }
                end = new NaviLatLng(glist.get(0).getLatLonPoint().getLatitude(), glist.get(0).getLatLonPoint().getLongitude());
                Utils.eNlat = end;
                LogUtils.log_E(end.getLatitude() + ":" + end.getLongitude());
                Intent gpsNaviIntent = new Intent(context, NewTestActivity.class);
                gpsNaviIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                gpsNaviIntent.putExtra("address", address_query);
                context.startActivity(gpsNaviIntent);
                ((Activity)context).finish();

            }
        });


        mSearch.getFromLocationNameAsyn(new GeocodeQuery(address_query, "北京"));
    }

    private String address_query;




    public class ViewHolder{
        TextView tv_start_time;
        TextView tv_info;
        TextView tv_info_e;
        TextView tv_company;
        TextView tv_company_e;
        TextView tv_address;
        TextView tv_address_e;
        ImageView ig_menu;
        RelativeLayout ll_menu;
        TextView tv_shoucang;
        TextView tv_remind;//是否设置提醒
        public RelativeLayout rl_menu;
    }
}
